import { HEADER_INDEX } from '../store/mutation-types/header/index'
import { BIND_FACEBOOK } from '../store/mutation-types/account/index'
import { getUserFromCookie, browserType, setToken, getCookie, removeToken } from '~/assets/js/global';
import { Message, MessageBox } from 'element-ui';
import NodeCookie from 'cookie';
import { basicConfig } from '~/assets/js/config'

export default function({store, redirect, req, isServer, route, res}) {
    var loggedIE = false;
    if (process.BROWSER_BUILD) {
        loggedIE = browserType(window.navigator.userAgent);
    } else {
        loggedIE = browserType(req.headers['user-agent']);
    }
    if (loggedIE) {
        return redirect('/below-ie8');
    }
    let routeName = route.name;
    if (routeName + 'Str' == 'nullStr') {
        return false;
    }
    let sToken = route.query.token ? route.query.token : null;
    if (route.query.token) {
        if (!isServer) {
            removeToken('token');
            setToken('token', sToken, {
                path: '/'
            });
        } else if (isServer) {
            res.setHeader('Set-Cookie', NodeCookie.serialize('token', sToken, {
                maxAge: 0
            }));
            res.setHeader('Set-Cookie', NodeCookie.serialize('token', sToken));
        }
    }

    let token;
    if (sToken) {
        token = sToken;
    } else {
        token = isServer ? getUserFromCookie(req, 'token') : getCookie('token');
    }

    if (!token) {
        token = 'undefined';
    }
    token = token + 'Str';

    let info = isServer ? getUserFromCookie(req, 'FBInfo') : getCookie('FBInfo');
    info = info + 'Str';
    let googleInfo = isServer ? getUserFromCookie(req, 'GoogleInfo') : getCookie('GoogleInfo');
    googleInfo = googleInfo + 'Str';

    if ((token == 'nullStr') || (token == 'undefinedStr')) {
        if (process.BROWSER_BUILD) {
            let token = getCookie('token');
            if (token) {
                removeToken('token');
                MessageBox.confirm('用户信息已过期,请重新登录', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let url = window.location.href.replace('http://', '');
                    url = url.replace(/[\?\&]?token=.*?\&/gi, '?').replace(/[\?\&]?token=.*/gi, '');
                    window.location.href = basicConfig.shopUrl + '?callback=' + url;
                }).catch(() => {

                });
            } else {
                let url = window.location.href.replace('http://', '');
                url = url.replace(/[\?\&]?token=.*?\&/gi, '?').replace(/[\?\&]?token=.*/gi, '');
                window.location.href = basicConfig.shopUrl + '?callback=' + url;
            }
        } else {
            let callback = req.headers.host + '' + route.fullPath;
            callback = callback.replace(/[\?\&]?token=.*?\&/gi, '?').replace(/[\?\&]?token=.*/gi, '');
            res.writeHead(302, {
                'Location': basicConfig.shopUrl + '?callback=' + callback
            });
            res.end();
        }
        return;
    }

    if (route.name != 'account') {
        if (route.name == 'adv-assets-assets-id') {
            if ( (!info || (info == 'nullStr') || (info == 'undefinedStr')) ) {
                if (process.BROWSER_BUILD) {
                    Message.warning({
                        message: '请先授权Facebook账号'
                    });
                }
                return redirect('/account');
            }
        }
        if ((!info || (info == 'nullStr') || (info == 'undefinedStr')) && (!googleInfo || (googleInfo == 'nullStr') || (googleInfo == 'undefinedStr'))) {
            if (process.BROWSER_BUILD) {
                Message.warning({
                    message: '请先授权Facebook账号，或者Google账号'
                });
            }
            store.commit('BIND_FACEBOOK', {
                bindFacebook: false
            });
            store.commit('BIND_GOOGLE', {
                bindGoogle: false
            });
            return redirect('/account');
        } else {
            if ((info && (info != 'nullStr') && (info != 'undefinedStr')) && (!googleInfo || (googleInfo == 'nullStr') || (googleInfo == 'undefinedStr'))) {
                store.commit('BIND_FACEBOOK', {
                    bindFacebook: true
                });
                if ((routeName == 'manager-google-id') || (routeName == 'statistics-indexGoogle')) {
                    return redirect('/account');
                }
            }
            if ((googleInfo && (googleInfo != 'nullStr') && (googleInfo != 'undefinedStr')) && (!info || (info == 'nullStr') || (info == 'undefinedStr'))) {
                store.commit('BIND_GOOGLE', {
                    bindGoogle: true
                });
                if ((routeName == 'manager-facebook-id') || (routeName == 'statistics')) {
                    return redirect('/account');
                }
            }
            if ((info && (info != 'nullStr') && (info != 'undefinedStr')) && (googleInfo && (googleInfo != 'nullStr') && (googleInfo != 'undefinedStr'))) {
                store.commit('BIND_FACEBOOK', {
                    bindFacebook: true
                });
                store.commit('BIND_GOOGLE', {
                    bindGoogle: true
                });
            }
        }
    }

    switch (routeName) {
    case 'index':
        store.commit(HEADER_INDEX, {
            headerIndex: 0
        });
        break;
    case 'create':
        store.commit(HEADER_INDEX, {
            headerIndex: 1
        });
        break;
    case 'statistics':
        store.commit(HEADER_INDEX, {
            headerIndex: 3
        });
        break;
    case 'statistics-indexGoogle':
        store.commit(HEADER_INDEX, {
            headerIndex: 3
        });
        break;
    case 'adv-assets-assets-id':
        store.commit(HEADER_INDEX, {
            headerIndex: 4
        });
        break;
    case 'account':
        store.commit(HEADER_INDEX, {
            headerIndex: 5
        });
        break;
    case 'feedback':
        store.commit(HEADER_INDEX, {
            headerIndex: 999
        });
        break;
    case 'manager-facebook-id':
        store.commit(HEADER_INDEX, {
            headerIndex: 2
        });
        break;
    case 'manager-google-id':
        store.commit(HEADER_INDEX, {
            headerIndex: 2
        });
        break;
    case 'create-edit-ad-group':
        store.commit(HEADER_INDEX, {
            headerIndex: 2
        });
        break;
    case 'create-edit-ad':
        store.commit(HEADER_INDEX, {
            headerIndex: 2
        });
        break;
    }
}